<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Z2glink_widget extends WP_Widget 
{ 
    /**
     * Register widget with WordPress.
     */
    function __construct() {
        parent::__construct(
                'z2glink_widget', // Base ID
                __('Z2G Link', 'text_domain'), // Name
                array( 'description' => __('Z2g Link Widget allows you to add a custom link on post and categories of a wordpress site. It has been made in the vision of using 2 diffrents sites and link all articles and categories to theire translations.', 'text_domain' ) ) // Args
        );
    }

    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance ) {
            /*$title = apply_filters( 'widget_title', $instance['title'] );

            echo $args['before_widget'];
            if ( ! empty( $title ) )
                    echo $args['before_title'] . $title . $args['after_title'];
            echo __( 'Hello, World!', 'text_domain' );
            echo $args['after_widget'];*/

        $url = $instance['site_url'];
        $text = $instance['display_text'];

        wp_enqueue_style('style', plugins_url().'/z2glinksites/css/style.css');
        
        if(is_category()){
            $category_id = get_query_var('cat');
            $opt = 'category-'.$category_id;

            $url_cat = get_option($opt);
            $url = $url_cat;
        }elseif(is_home())
        {
            //echo 'HOME : ';
            //echo $instance['site_url'];
        }
        else 
        {
            global $post;
           $url = esc_attr(get_post_meta($post->ID,'_z2glink',true)); 
        }
        ?>
            <div id="z2glinksite-widget">
                <a href="<?=$url?>"><?=$text?></a>
            </div> 
        <?php
    }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form( $instance ) {
        $defaults =array('display_text'=>'FR',
                         'site_url'=>'http://definirlafrontiere.ca');
        $instance= wp_parse_args($instance,$defaults);

         ?>
         <p>
         <label for="<?php echo $this->get_field_id( 'display_text' ); ?>"><?php _e( 'Displayed Text:' ); ?></label> 
         <input class="widefat" id="<?php echo $this->get_field_id( 'display_text' ); ?>" name="<?php echo $this->get_field_name( 'display_text' ); ?>" type="text" value="<?php echo esc_attr( $instance['display_text'] ); ?>" />
         </p>
         <p>
         <label for="<?php echo $this->get_field_id( 'site_url' ); ?>"><?php _e( 'Site URL:' ); ?></label> 
         <input class="widefat" id="<?php echo $this->get_field_id( 'site_url' ); ?>" name="<?php echo $this->get_field_name( 'site_url' ); ?>" type="text" value="<?php echo esc_attr( $instance['site_url'] ); ?>" />
         </p>
         <?php 
    }

    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update( $new_instance, $old_instance ) {
            $new_instance['display_text'] = ( ! empty( $new_instance['display_text'] ) ) ? strip_tags( $new_instance['display_text'] ) : 'FR';

            return $new_instance;
    }
}

?>
